package com.jiyun.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.common.model.Evaluate;
import com.jiyun.common.model.Play;
import com.jiyun.common.vo.R;
import com.jiyun.fegin.PlayFegin;
import com.jiyun.service.EvaluateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: 2210A-project1
 * @package: com.jiyun.controller
 * @className: MongodbController
 * @author: 耿嘉军
 * @date: 2023/9/7 20:28
 * @version: 1.0
 */
@RestController
@RequestMapping("mongodb")
@Slf4j
public class MongodbController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EvaluateService evaluateService;

    @Autowired
    private PlayFegin playFegin;

    /**
     * 添加评论  根据pid 是否等于 0 判断是不是 根级别的平论
     *
     */
    @RequestMapping("saveEav")
    public R saveEav(Evaluate evaluate, HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            String play = (String) redisTemplate.opsForHash().get("play", token);
            Play play1 = JSONUtil.toBean(play, Play.class);
            evaluate.setCreateTime(new Date());
            evaluate.setPlayId(play1.getPid()+"");
            mongoTemplate.insert(evaluate);
            return R.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 查询评论
     */

    @RequestMapping("findEva")
    public R findEva(String courseId){
        try {
            // 获取所有评论数据
            Query query = Query.query(Criteria.where("courseId").is(courseId));
            List<Evaluate> comments = mongoTemplate.find(query,Evaluate.class);
            for (Evaluate comment : comments) {
                Play playById = playFegin.findPlayById(Integer.parseInt(comment.getPlayId()));
                comment.setPlayName(playById.getPname());
            }
            List<Evaluate> rootComments = comments.stream()
                    .filter(comment -> comment.getPid() == null || comment.getPid().isEmpty()|| "0".equals(comment.getPid()))
                    .collect(Collectors.toList());

            for (Evaluate rootComment : rootComments) {
                rootComment.generateComments(comments, rootComment.getId());
            }
            return R.ok(rootComments);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }




}
